package ml.kupin.order.schedule;

import ml.kupin.common.constant.KuPinConstant;
import ml.kupin.common.utils.DateUtils;
import ml.kupin.common.utils.KuPinResult;
import ml.kupin.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * Created by 小柯(Allen Kerr) on 17.5.9 20:22.
 */
public class OrderSchedule {

    @Autowired
    private OrderService orderService;

    @Value("${order_not_pay_lifetime}")
    private int order_not_pay_lifetime;

    private Logger logger = LoggerFactory.getLogger(OrderSchedule.class);

    public void closeNotPayOrder() {
        logger.info("执行 “关闭超时未支付订单” 任务");
        KuPinResult kuPinResult = orderService.updateStatus(null, KuPinConstant.Order_Closed,
                KuPinConstant.Order_Not_Pay, DateUtils.getTimeAgo(order_not_pay_lifetime));
//        KuPinResult kuPinResult = orderService.updateNotPayOrderStatus();
        if (kuPinResult.getStatus() == KuPinConstant.KuPinResult_status_success) {
            Integer updateCount = (Integer) kuPinResult.getData();
            logger.info("关闭 " + updateCount + " 个超时未支付订单。");
        } else {
            logger.info("未发现存在超时未支付订单。");
        }
    }

}
